package com.wlyuan.open.domain.api;

import com.wlyuan.core.domain.valueobj.UserId;
import com.wlyuan.core.marker.NamedStatus;
import com.wlyuan.open.domain.api.valueobj.OpenApiInfoId;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Optional;

/**
 * @author lijing
 * @description
 */
@Data
public class OpenApiInfo implements Serializable {
    private static final long serialVersionUID = 8571869621786229717L;

    private OpenApiInfoId id;

    @ApiModelProperty("clientId")
    private Long clientId;
    @ApiModelProperty("接口id")
    private Long apiId;
    @ApiModelProperty("状态 0 禁用 1 启用 2 待审核 4 拒绝")
    private NamedStatus status;
    @ApiModelProperty("总调用次数")
    private Integer totalNum;
    @ApiModelProperty("使用调用次数")
    private Integer leaveNum;
    @ApiModelProperty("创建时间")
    private LocalDateTime createAt;
    @ApiModelProperty("更新时间")
    private LocalDateTime updateAt;
    @ApiModelProperty("创建人")
    private UserId createBy;
    @ApiModelProperty("更新人")
    private String updateBy;
    @ApiModelProperty("拒绝原因")
    private String reason;
    @ApiModelProperty("公司名称")
    private String clientName;
    @ApiModelProperty("接口名称")
    private String apiName;
    @ApiModelProperty("接口描述")
    private String apiRemark;

    public boolean hasEnable() {
        return Optional.ofNullable(status).map(status -> NamedStatus.ENABLED.equals(status)).orElse(false);
    }

    public boolean hasDisable() {
        return Optional.ofNullable(status).map(status -> NamedStatus.DISABLED.equals(status)).orElse(false);
    }

    public boolean hasExists() {
        return hasEnable() || hasDisable();
    }

    public boolean hasUse() {
        if (this.totalNum == null) {
            return true;
        }
        // -1 表示无限次数
        return this.totalNum > 0 || this.totalNum == -1;
    }

    public void decreaseTotalNum() {
        //总次数减的时候，不能小于0
        if (this.totalNum > 0) {
            this.totalNum--;
        }
        this.updateAt = LocalDateTime.now();
    }

    public void increaseLeaveNum() {
        if (this.leaveNum == null) {
            return;
        }
        //使用次数不能大于总次数
        if (this.leaveNum <= this.totalNum) {
            this.leaveNum++;
        }
        this.updateAt = LocalDateTime.now();
    }

    public OpenApiInfo save(UserId userId, Integer totalNum, Integer leaveNum) {
        this.status = NamedStatus.AUDITING;
        this.totalNum = totalNum;
        this.leaveNum = leaveNum;
        this.createBy = userId;
        this.reason = "";
        this.createAt = LocalDateTime.now();
        return this;
    }

    public OpenApiInfo examine(String user) {
        this.status = NamedStatus.ENABLED;
        this.updateBy = user;
        this.reason = "";
        this.updateAt = LocalDateTime.now();
        return this;
    }

    public OpenApiInfo reject(String user, String reason) {
        this.status = NamedStatus.REJECTED;
        this.updateBy = user;
        this.reason = reason;
        this.updateAt = LocalDateTime.now();
        return this;
    }

    public OpenApiInfo enable(String user) {
        this.status = NamedStatus.ENABLED;
        this.updateBy = user;
        this.updateAt = LocalDateTime.now();
        return this;
    }

    public OpenApiInfo disable(String user) {
        this.status = NamedStatus.DISABLED;
        this.updateBy = user;
        this.updateAt = LocalDateTime.now();
        return this;
    }
}
